package calm.museum;


import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import jenamuseum.annotation.Annotation;
import jenamuseum.annotation.AnnotationBase;
import jenamuseum.annotation.AnnotationList;
import jenamuseum.annotation.AnnotationListResource;
import jenamuseum.annotation.AnnotationResource;
import jenamuseum.annotation.AnnotationVideo;

import org.restlet.resource.ClientResource;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class RestletAdapter {
	public ClientResource cr;
	public ClientResource cr2;
	public AnnotationResource annotationRes;	
	public AnnotationListResource annotationListRes;
	public Context mCtx;
	/* Create a two new connector and wrap it to the interface definition, there is one wrapper by datatype (ie. one wrapper 
	 * to get all the annotations by artwork and one wrapper to send a unique annotation object*/
	public RestletAdapter()
	{
		cr = new ClientResource(mainActivity.serverURL + "annotation");
		cr2 = new ClientResource(mainActivity.serverURL + "annotationList");

		 annotationRes = cr.wrap(AnnotationResource.class);
		 annotationListRes = cr2.wrap(AnnotationListResource.class);
		 
	}
	
	public void addAnnotation(Annotation annotation)
	{
		annotationRes.post(annotation);
	}

	
	public AnnotationList getAllAnnotation(String artwork)
	{
		// Get the list of textual annotation from server
		AnnotationList serverAnnotation = annotationListRes.getAllAnnotations(artwork);
		// Add annotation stored locally (audio and video annotation) to that list
		return AddVideoAndAudio(serverAnnotation);
	}
	
	public Annotation get(String id)
	{
		return annotationRes.getAnnotation(id);
	}
	
	
    public AnnotationArrayAdapter fetchAllAnnotation(String artwork)
    {
    	//ArrayList<Annotation> annotationArray = getAllAnnotation(artwork);
		//return new AnnotationArrayAdapter(mCtx, R.layout.row_layout,annotationArray );
    	return null;
    
    }
    
    /* List audio and video file in the ressources directory
     * in order to add them to the annotationList
     */
    public List<String> listFileInFP()
    {	List<String> fileList = new ArrayList<String>();
    	String filePath = "/ressources/";
    	String envStorage = Environment.getExternalStorageDirectory()+ filePath;
    	File f = new File(envStorage);
    	String[] fList = f.list();
    	for(int i=0; i <fList.length;i++)
    	{
    		fileList.add(envStorage + fList[i]);
    	}
    	
    	for(String  s: fileList)
    	{
    		Log.e("TEST", s);
    		
    	}
		return fileList;
    	
    }
    
    /*
     * Add audio and video annotation stored locally to the list
     */
    public AnnotationList AddVideoAndAudio(AnnotationList liste)
    {
    	List<String> listeF = listFileInFP();
    	for(String i : listeF)
    	{
    		if(i.contains("vid"))
    		{
    		AnnotationVideo  vidz= new AnnotationVideo();
    		vidz.setUrl(i);
    		liste.add(vidz);
    		}
    		}
    	return liste;
    }
    
    


}
